package cn.zswltech.gruul.common.util;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;


public class AESUtil {
    private static String MODE = "AES/CBC/PKCS5Padding";
    private static String AES = "AES";
    private static String CHARSET = "utf-8";

    private static Logger logger = LoggerFactory.getLogger(AESUtil.class);

    public AESUtil() {
    }


    public static String encrypt(String content, String aesPwd, String aesIvp) {
        String eContent = null;

        try {
            if (content == null || aesPwd == null) {
                return eContent;
            }

            if (aesPwd.length() != 16) {
                return eContent;
            }

            byte[] aesPwdBytes = aesPwd.getBytes("utf-8");
            SecretKeySpec keySpec = new SecretKeySpec(aesPwdBytes, AES);
            Cipher cipher = Cipher.getInstance(MODE);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(aesIvp.getBytes());
            cipher.init(1, keySpec, ivParameterSpec);
            byte[] contentBytes = cipher.doFinal(content.getBytes(CHARSET));
            eContent = parseByte2HexStr(contentBytes);
        } catch (Exception e) {
            logger.error("AES encrypt error", e);
        }

        return eContent;
    }


    public static String decrypt(String content, String aesPwd, String aesIvp) {
        String dContent = null;
        if (content != null && aesPwd != null) {
            try {
                byte[] aesPwdBytes = aesPwd.getBytes(CHARSET);
                SecretKeySpec keySpec = new SecretKeySpec(aesPwdBytes, AES);
                Cipher cipher = Cipher.getInstance(MODE);
                IvParameterSpec ivParameterSpec = new IvParameterSpec(aesIvp.getBytes());
                cipher.init(2, keySpec, ivParameterSpec);
                byte[] contentBytes = cipher.doFinal(parseHexStr2Byte(content));
                dContent = new String(contentBytes, CHARSET);
            } catch (Exception e) {
                logger.error("AES decrypt error", e);
            }

            return dContent;
        } else {
            return dContent;
        }
    }

    public static String parseByte2HexStr(byte[] var0) {
        StringBuilder var1 = new StringBuilder();

        for(int var2 = 0; var2 < var0.length; ++var2) {
            String var3 = Integer.toHexString(var0[var2] & 255);
            if (var3.length() == 1) {
                var3 = '0' + var3;
            }

            var1.append(var3.toLowerCase());
        }

        return var1.toString();
    }

    public static byte[] parseHexStr2Byte(String var0) {
        if (StringUtils.isEmpty(var0)) {
            return null;
        } else {
            byte[] var1 = new byte[var0.length() / 2];

            for(int var2 = 0; var2 < var0.length() / 2; ++var2) {
                int var3 = Integer.parseInt(var0.substring(var2 * 2, var2 * 2 + 1), 16);
                int var4 = Integer.parseInt(var0.substring(var2 * 2 + 1, var2 * 2 + 2), 16);
                var1[var2] = (byte)(var3 * 16 + var4);
            }

            return var1;
        }
    }

    public static String hideString(String var0, int var1, int var2) {
        if (StringUtils.isEmpty(var0)) {
            return var0;
        } else {
            int var3 = var0.length();
            if (var1 < 0) {
                var1 = 0;
            }

            if (var1 > var3) {
                var1 = var3;
            }

            if (var2 < 0) {
                var2 = 0;
            }

            if (var2 > var3 - var1) {
                var2 = var3 - var1;
            }

            StringBuffer var4 = new StringBuffer();

            for(int var5 = 0; var5 < var2; ++var5) {
                var4.append("*");
            }

            var0 = var0.substring(0, var1) + var4.toString() + var0.substring(var1 + var2);
            return var0;
        }
    }
}
